<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  Zend
 * @package   Zend_DataGrid
 * @subpackage Column
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 * @version   $Id: HyperLink.php 6137 2008-09-13 17:37:27Z azoungrana $
 */

/** @see Zend_DataGrid_Column */
require_once 'Zend/DataGrid/Column.php';



/**
 * Zend_DataGrid_Column_HyperLink
 * The hyperlink column contains a hyperlink for each item in the column. You can set the
 * text and the url of the hyperlink by {@link setText} and {@link setUrl}, respectively.
 * You can also bind the text and url to specific data field in datasource by setting
 * {@link setDataUrlField}. Itcan be formatted before rendering according to {@link getDataUrlFormat} value.
 *
 * If both {@link getText} and {@link getDataFormatString} are present, the latter takes precedence.
 * The same rule applies to {@link getUrl} and {@link getDataUrlFormat} values.
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @subpackage Column
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_DataGrid_Column_HyperLink extends Zend_DataGrid_Column
{
    /**#@+
     * The list of target
     */
    const TARGET_BLANK = '_blank';
    const TARGET_PARENT = '_parent';
    const TARGET_SELF = '_self';
    const TARGET_TOP = '_top';
    /**#@-*/

    /**
     * The fields from the data source to bind to the url of hyperlink element
     *
     * @var array
     */
    protected $_dataUrlFields = array();

    /**
     * The formatting string used to component how the navigate url of hyperlink will be displayed.
     *
     * @var string
     */
    protected $_dataUrlFormat = null;

    /**
     * The static url to display
     *
     * @var string
     */
    protected $_staticUrl = null;

    /**
     * The static text to display
     *
     * @var string
     */
    protected $_staticText = null;

    /**
     * The target of url
     *
     * @var string
     */
    protected $_target = null;


    /**
     * Sets the field name from the data source to bind to the url of hyperlink element
     *
     * @param string $name
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function setDataUrlField($name)
    {
        $this->clearDataUrlField();
        $this->addDataUrlField($name);
        return $this;
    }

    /**
     * Gets the fields names from the data source to bind to the url of hyperlink element
     *
     * @return array
     */
    public function getDataUrlFields()
    {
        return $this->_dataUrlFields;
    }

    /**
     * Adds the field name from the data source to bind to the url of hyperlink element
     *
     * @param string $name
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function addDataUrlField($name)
    {
        $this->_dataUrlFields[] = (string) $name;
        return $this;
    }

    /**
     * Adds the fields names from the data source to bind to the url of hyperlink element
     *
     * @param string $names
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function addDataUrlFields(array $names)
    {
        foreach ($names as $name)
            $this->addDataUrlField($name);

        return $this;
    }

    /**
     * Clear the field names
     *
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function clearDataUrlField()
    {
        $this->_dataUrlFields = array();
        return $this;
    }

    /**
     * Set the formatting string used to control how the navigate url
     * of hyperlink will be displayed.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function setDataUrlFormat($value)
    {
        $this->_dataUrlFormat = (string) $value;
        return $this;
    }

    /**
     * Gets the formatting string used to control how the navigate url
     * of hyperlink will be displayed.
     *
     * @return string
     */
    public function getDataUrlFormat()
    {
        return $this->_dataUrlFormat;
    }

    /**
     * Sets the static URL to link to when the hyperlink is clicked.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function setStaticUrl($value)
    {
        $this->_staticUrl = (string) $value;
        return $this;
    }

    /**
     * Gets the static URL to link to when the hyperlink is clicked.
     *
     * @param string $value
     */
    public function getStaticUrl()
    {
        return $this->_staticUrl;
    }

    /**
     * Sets the static text caption of the hyperlink.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function setStaticText($value)
    {
        $this->_staticText = (string) $value;
        return $this;
    }

    /**
     * Gets the static text caption of the hyperlink
     *
     * @return string
     */
    public function getStaticText()
    {
        return $this->_staticText;
    }

    /**
     * Sets the target window or frame to display the Web page content
     * linked to when the hyperlink is clicked.
     *
     * The target window, valid values include '_blank', '_parent', '_self', '_top' and empty string.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_HyperLink
     */
    public function setTarget($value)
    {
        $targetTypes = array(self::TARGET_BLANK, self::TARGET_PARENT, self::TARGET_SELF, self::TARGET_TOP);
        if (in_array($value, $targetTypes))
            $this->_target = $value;
        else
            throw new Zend_DataGrid_Column_Exception('The target must take its value in :'. implode(' ,', $targetTypes));

        return $this;
    }

    /**
     * Sets the target window or frame to display the Web page content
     * linked to when the hyperlink is clicked.
     *
     * @return string
     */
    public function getTarget()
    {
        return $this->_target;
    }


}
